package com.krmes.mes.app;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build.VERSION;
import android.os.Environment;


import com.krmes.mes.base.BaseApplication;
import com.krmes.mes.cache.DataCleanManager;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;




public class AppContext
        extends BaseApplication
{
    private static AppContext instance;
    private static long lastClickTime;
    public static String loginCode;
    public static String roleCode;
    public static String roleName;
    public static String userName;

    public static AppContext getInstance()
    {
        if (instance ==null ){
            instance = new AppContext();
            instance.onCreate();
        }
        return instance;
    }

    public static String getIp()
    {
        try{
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
//                    if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
//
//                        return inetAddress.getHostAddress().toString();
//                    }
                }
            }
        }catch (SocketException e) {
            // TODO: handle exception
//            Utils.log("WifiPreference IpAddress---error-" + e.toString());

        }

        return null;
    }


    private void init()
    {
        //OkHttpClient client = new OkHttpClient();

    }

    /* Error */
    public static boolean isFastClick()
    {
//        long time = System.currentTimeMillis();
//        if((time - lastClickTime) < 0x3e8) {
//            (time - lastClickTime) [cmp] 0x3e8 = 0x1;
//        } else {
//            lastClickTime = time;
//        }
//        return localconst/41, localconst/41 = 0x0;
        long time = System.currentTimeMillis();
        if ( time - lastClickTime < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static boolean isMethodsCompat(int paramInt)
    {
        return VERSION.SDK_INT >= paramInt;
    }

    public void clearAppCache()
    {
        DataCleanManager.cleanInternalCache(this);
        DataCleanManager.cleanCustomCache(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Camera/");
        Iterator localIterator = getProperties().keySet().iterator();
        for (;;)
        {
            if (!localIterator.hasNext()) {
                return;
            }
            String str = localIterator.next().toString();
            if (str.startsWith("temp")) {
                removeProperty(new String[] { str });
            }
        }
    }

    public PackageInfo getPackageInfo()
    {
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0x0);
        } catch(NameNotFoundException e) {
            e.printStackTrace(System.err);
        }
        if(info == null) {
             info = new PackageInfo();
        }
        return info;
    }

    public Properties getProperties()
    {
        return AppConfig.getAppConfig(this).get();
    }

    public String getProperty(String paramString)
    {
        return AppConfig.getAppConfig(this).get(paramString);
    }
    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;
        AppConfig.getAppConfig(this).initProperties(this);
        init();
    }

    public void removeProperty(String... paramVarArgs)
    {
        AppConfig.getAppConfig(this).remove(paramVarArgs);
    }
}
